﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{
	/**
	  This class represents a HashTable of Identifier -> PlayerShapeObject
	*/
	public class ShapeHashTable{
		
		private var dataTable:Vector.<Vector.<PlayerShapeObject> >;
		private var indexTable:Vector.<Vector.<uint> >;
		private var sceneList:Vector.<uint>;
		
		public function ShapeHashTable(){
			dataTable = new Vector.<Vector.<PlayerShapeObject> >();
			indexTable = new Vector.<Vector.<uint> >();
			sceneList = new Vector.<uint>();
		}
		
		/**
		  Inserts obj inside the hash table
		  @param obj PlayerShapeObject to insert inside the hashTable
		*/
		public function insert(obj:PlayerShapeObject):void{
							// We look for this scene
			var scenePos:int = sceneList.indexOf(obj.getStageID());
			var objPos:int = 0;
			
			// If scene wasn't inside, we insert it
			if(scenePos < 0){
				// Now we have the index of the scene
				scenePos = sceneList.push(obj.getStageID()) -1;
			}
			
			// If such position didnt exist in indexTable,
			// we insert new Vector in this row
			if(scenePos >= indexTable.length){
				objPos = indexTable.push(new Vector.<uint>()) -1;
			}
			
			// We look for the obj's ID inside the indexTable
			objPos = indexTable[scenePos].indexOf(obj.getID());
			
			// If obj wasn't inside indexTable's row, we insert it
			if(objPos < 0){
				// Now we have the index of the object, for sure
				objPos = indexTable[scenePos].push(obj.getID()) -1;
			}
			
			// If such position didnt exist in dataTable,
			// we insert new row
			if(scenePos >= dataTable.length){
				dataTable.push(new Vector.<PlayerShapeObject>());
			}
			
			// If the ID didn't exist in the row, we insert it
			if(objPos >= dataTable[scenePos].length){
				dataTable[scenePos].push(obj);
			}else{ // If it did exist, the we just copy it
				dataTable[scenePos][objPos] = obj;
			}
			
		}
		
		/**
		  Gets the object identified by ID. Note ID must be formated as specified
		  in PlayerShapeObject. This function does NOT check if object does not
		  exists.
		  @param id String with ID of the object to get
		*/
		public function get(id:String):PlayerShapeObject{
			var scenePos:int = sceneList.indexOf(PlayerShapeObject.SceneOf(id));
			return dataTable[scenePos]
							[indexTable[scenePos].indexOf(PlayerShapeObject.IDof(id))] as PlayerShapeObject;
		}
		
	}
}